package com.viphuli.app.tool.handler;

import java.util.List;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.ButterKnife;

import com.offroader.utils.DateUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.page.ArrangeRecordDetailPage;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.bean.part.ArrangeInfo;
import com.viphuli.app.tool.bean.part.ArrangeRecordDetail;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.Constants.ArrangeRecordStatus;
import com.viphuli.app.tool.common.Constants.ArrangeRecordType;
import com.viphuli.app.tool.common.Constants.UserType;
import com.viphuli.app.tool.fragment.ArrangeRecordDetailExchangeFragment;
import com.viphuli.app.tool.utils.ImageUtils;

public class ArrangeRecordDetailExchangeResponseHandler extends
		MyBaseHttpResponseHandler<ArrangeRecordDetailExchangeFragment, ArrangeRecordDetailPage> {

	@Override
	public void deal() {

		ArrangeRecordDetail detail = page.getArrangeRecordDetail();

		//赋值
		caller.setDetail(detail);

		//排班信息
		List<ArrangeInfo> arrangeInfoList = detail.getArrangeInfoList();
		AccountUser fromUser = arrangeInfoList.get(0).getFromUser();
		AccountUser toUser = arrangeInfoList.get(1).getToUser();

		caller.getFromUserName().setText(fromUser.getUserName());
		ImageUtils.load(caller.getFromUserImg(), fromUser.getPortrait());
		caller.getToUserName().setText(toUser.getUserName());
		ImageUtils.load(caller.getToUserImg(), toUser.getPortrait());

		//排班类型信息

		for (int i = 0; i < arrangeInfoList.size(); i++) {

			ArrangeType arrangeType = arrangeInfoList.get(i).getArrangeTypeinfo();

			if (i == 0) {

				View toLayout = caller.getArrangeToLayout();

				View iconBg = ButterKnife.findById(toLayout, R.id.id_home_item_icon_bg);
				ImageView iconImg = ButterKnife.findById(toLayout, R.id.id_home_item_icon_img);
				TextView iconName = ButterKnife.findById(toLayout, R.id.id_home_item_icon_name);
				TextView itemTime = ButterKnife.findById(toLayout, R.id.id_home_item_time);
				TextView itemDesc = ButterKnife.findById(toLayout, R.id.id_home_item_desc);

				iconBg.setBackgroundColor(Color.parseColor(arrangeType.getColor()));
				ImageUtils.load(iconImg, arrangeType.getIcon());
				iconName.setText(arrangeType.getName());
				itemDesc.setText(arrangeType.getDuration());
				itemTime.setText(DateUtils.formatDay(arrangeType.getStart()));

			} else if (i == 1) {

				View fromLayout = caller.getArrangeFromLayout();

				View iconBg = ButterKnife.findById(fromLayout, R.id.id_home_item_icon_bg);
				ImageView iconImg = ButterKnife.findById(fromLayout, R.id.id_home_item_icon_img);
				TextView iconName = ButterKnife.findById(fromLayout, R.id.id_home_item_icon_name);
				TextView itemTime = ButterKnife.findById(fromLayout, R.id.id_home_item_time);
				TextView itemDesc = ButterKnife.findById(fromLayout, R.id.id_home_item_desc);

				iconBg.setBackgroundColor(Color.parseColor(arrangeType.getColor()));
				ImageUtils.load(iconImg, arrangeType.getIcon());
				iconName.setText(arrangeType.getName());
				itemDesc.setText(arrangeType.getDuration());
				itemTime.setText(DateUtils.formatDay(arrangeType.getStart()));

			}

		}

		caller.getRecordTime().setText(DateUtils.format(detail.time()));
		caller.getRecordName().setText(detail.getName());

		//排班类型
		ArrangeRecordType type = ArrangeRecordType.getType(detail.getType());

		caller.getRecordType().setText(type.getName());

		if (ArrangeRecordType.arrange.equals(type)) {

			caller.getRecordIconBg().setBackgroundResource(R.drawable.ic_arrange_record_arrange);

		} else if (ArrangeRecordType.arrange_cancel.equals(type)) {

			caller.getRecordIconBg().setBackgroundResource(R.drawable.ic_arrange_record_arrange);

		} else if (ArrangeRecordType.exchange.equals(type)) {

			caller.getRecordIconBg().setBackgroundResource(R.drawable.ic_arrange_record_exchange);

		} else if (ArrangeRecordType.exchange_cancel.equals(type)) {

			caller.getRecordIconBg().setBackgroundResource(R.drawable.ic_arrange_record_exchange);

		} else if (ArrangeRecordType.replace.equals(type)) {

			caller.getRecordIconBg().setBackgroundResource(R.drawable.ic_arrange_record_exchange);

		} else if (ArrangeRecordType.replace_cancel.equals(type)) {

			caller.getRecordIconBg().setBackgroundResource(R.drawable.ic_arrange_record_exchange);

		}

		//排班状态
		ArrangeRecordStatus status = ArrangeRecordStatus.getStatus(detail.getStatus());

		if (ArrangeRecordStatus.wait_accept.equals(status)) {

			caller.getBtnAccept().setVisibility(View.VISIBLE);
			caller.getBtnReject().setVisibility(View.VISIBLE);

			caller.getRecordStatus().setText("待通过");
			caller.getRecordStatus().setTextColor(caller.getResources().getColor(R.color.cl_arrange_record_status_wait_accept));

		} else if (ArrangeRecordStatus.cancel.equals(status)) {

			caller.getRecordStatus().setText("已撤销");
			caller.getRecordStatus().setTextColor(caller.getResources().getColor(R.color.cl_arrange_record_status_wait_accept));

		} else if (ArrangeRecordStatus.reject.equals(status)) {

			caller.getRecordStatus().setText("未通过");
			caller.getRecordStatus().setTextColor(caller.getResources().getColor(R.color.cl_arrange_record_status_reject));

		} else if (ArrangeRecordStatus.accept.equals(status)) {

			caller.getRecordStatus().setText("已通过");
			caller.getRecordStatus().setTextColor(caller.getResources().getColor(R.color.cl_arrange_record_status_accept));

		} else if (ArrangeRecordStatus.effect.equals(status)) {

			caller.getRecordStatus().setText("已生效");
			caller.getRecordStatus().setTextColor(caller.getResources().getColor(R.color.cl_arrange_record_status_wait_accept));

		}

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(caller.getActivity());

		if (tokenKeeper.isLogin()) {

			//显示撤销 - 发起者是自己显示撤销，并且不是已撤销状态
			if (!ArrangeRecordStatus.cancel.equals(status) && arrangeInfoList.get(0).getFromUser().getUid().equals(tokenKeeper.getOpenId())) {
				//caller.getBtnCancel().setVisibility(View.VISIBLE);
				caller.getItemRemarkLayout().setVisibility(View.VISIBLE);
			}

			//显示确定和拒绝 - 待通过状态时 并且 发起者是管理员显示确定和拒绝按钮
			int userType = tokenKeeper.getUser().getUserType();
			if (ArrangeRecordStatus.wait_accept.equals(status) && UserType.arrange_admin.equals(UserType.getType(userType))) {
				caller.getBtnAccept().setVisibility(View.VISIBLE);
				caller.getBtnReject().setVisibility(View.VISIBLE);
				caller.getItemRemarkLayout().setVisibility(View.VISIBLE);
			}

		}

		//		caller.getBtnCancel().setVisibility(View.VISIBLE);
		//		caller.getBtnAccept().setVisibility(View.VISIBLE);
		//		caller.getBtnReject().setVisibility(View.VISIBLE);

	}
}
